import { BasicColumn, FormSchema } from '/@/components/Table';
import { h, ref } from 'vue';
import { Switch } from 'ant-design-vue';
import { editMeetingRoom } from '/@/api/admin/meeting';

import { useMessage } from '/@/hooks/web/useMessage';
const { createMessage } = useMessage();
// import { SaveMeetingAction } from '/@/api/admin/model/meetingModel';
export const columns: BasicColumn[] = [
  {
    title: '会议室名称',
    dataIndex: 'name',
  },
  {
    title: '设备信息',
    dataIndex: 'deviceids',
  },
  {
    title: '会议室地址',
    dataIndex: 'address',
  },
  {
    title: '人数容量',
    dataIndex: 'capacity',
  },
  {
    title: '当前状态',
    dataIndex: 'enable',
    customRender: ({ record }) => {
      const { id, deviceids, name, address, capacity, enable } = record;
      const loading = ref(true);
      if (Reflect.has(record, 'enable')) {
        loading.value = false;
      }

      return h(Switch, {
        checked: record.enable === 1,
        loading: loading.value,
        async onChange(checked: boolean) {
          const newStatus = checked ? 1 : 0;

          try {
            await editMeetingRoom({
              id,
              deviceids,
              name,
              address,
              capacity,
              enable: newStatus,
            });
            record.enable = newStatus;
            createMessage.success(`已成功修改状态`);
          } catch {
            createMessage.error(`修改状态失败`);
            record.enable = enable;
          }
        },
      });
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    component: 'Input',
    label: '会议室名称',
    colProps: {
      span: 6,
    },
  },
  {
    field: 'address',
    component: 'Input',
    label: '会议室地址',
    colProps: {
      span: 6,
    },
  },
];

export const addFormSchema: FormSchema[] = [
  {
    field: 'name',
    component: 'Input',
    label: '会议室名称',
    required: true,
  },
  {
    field: 'deviceids',
    component: 'CheckboxGroup',
    label: '设备信息',
    // required: true,
    slot: 'info',
  },
  {
    field: 'address',
    component: 'Input',
    label: '会议室地址',
    required: true,
  },
  {
    field: 'capacity',
    component: 'InputNumber',
    label: '容纳人数',
    required: true,
  },
  {
    field: 'enable',
    component: 'Select',
    label: '是否启用',
    defaultValue: 1,
    required: true,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 },
      ],
    },
  },
];
